package com.lzq.model.mediator;

/**
 * @author : modify
 * @version : v1.0
 * @description :
 * @createTime : 2022/11/29 10:09
 * @updateUser : modify
 * @updateTime : 2022/11/29 10:09
 * @updateRemark : 中介者模式
 */
public abstract class BaseMediator {
    /**
     * 交流方法
     *
     * @param message
     * @param person
     */
    public abstract void constact(String message, BasePerson person);
}

abstract class BasePerson {
    protected String name;
    protected BaseMediator mediator;

    public BasePerson(String name, BaseMediator mediator) {
        this.name = name;
        this.mediator = mediator;
    }
}

/**
 * 租户类
 */
class Tenant extends BasePerson {

    public Tenant(String name, BaseMediator mediator) {
        super(name, mediator);
    }

    /**
     * 与中介联系
     *
     * @param message
     */
    public void constact(String message) {
        mediator.constact(message, this);
    }

    /**
     * 获取信息
     *
     * @param message
     */
    public void getMessage(String message) {
        System.out.println("租房者" + name + "获取到的信息：" + message);
    }
}

class HouseOwner extends BasePerson {

    public HouseOwner(String name, BaseMediator mediator) {
        super(name, mediator);
    }

    /**
     * 与中介联系
     *
     * @param message
     */
    public void constact(String message) {
        mediator.constact(message, this);
    }

    /**
     * 获取信息
     *
     * @param message
     */
    public void getMessage(String message) {
        System.out.println("房主" + name + "获取到的信息：" + message);
    }
}

class MediatorStructure extends BaseMediator {
    private HouseOwner houseOwner;
    private Tenant tenant;

    public HouseOwner getHouseOwner() {
        return houseOwner;
    }

    public Tenant getTenant() {
        return tenant;
    }

    public void setHouseOwner(HouseOwner houseOwner) {
        this.houseOwner = houseOwner;
    }

    public void setTenant(Tenant tenant) {
        this.tenant = tenant;
    }

    @Override
    public void constact(String message, BasePerson person) {
        if (person == houseOwner) {
            tenant.getMessage(message);
        } else {
            houseOwner.getMessage(message);
        }
    }
}

class Test {
    public static void main(String[] args) {
        MediatorStructure mediator = new MediatorStructure();
        Tenant tenant = new Tenant("黄月月", mediator);
        HouseOwner houseOwner = new HouseOwner("黄月", mediator);
        mediator.setHouseOwner(houseOwner);
        mediator.setTenant(tenant);
        tenant.constact("需要租三室的房子");
        houseOwner.constact("我这有三室的房子，你需要租吗？");
    }
}